﻿using UnityEngine;
using System.Collections;
using NewEditorNS;
using System;
using System.IO;
using LitJson;
using System.Collections.Generic;
using SongeNS;

public class PanoramaScript : MonoBehaviour
{


    public Camera theCamera = null;
    public Renderer PanoRenderer;
    //UIWidget _theWidget = null;
    //UIWidget theWidget
    //{
    //    get
    //    {
    //        if (_theWidget == null)
    //            _theWidget = GetComponent<UIWidget>();
    //        return _theWidget;
    //    }
    //}

    public void Start()
    {
        //theWidget.onChange += onWidthChange;
        theCamera.farClipPlane = 10000f;
        //theCamera.cullingMask = -1;
        theCamera.fieldOfView = 100f;

        EventManager.instance.Register(null, OnEvent);

    }

    /// <summary>
    /// Refresh Street
    /// </summary>
    /// <param name="eventName"></param>
    /// <param name="param"></param>
    void OnEvent(string eventName, object param)
    {
        if (eventName == UICONST.REFRESH_STREET)
        {
            GetBaiduPano();
        }
    }

    //void onWidthChange()
    //{
    //    if (theCamera == null)
    //        return;
    //    float l = theWidget.leftAnchor.absolute / (float)Screen.width;
    //    float t = theWidget.bottomAnchor.absolute / (float)Screen.height;
    //    float w = theWidget.width / (float)Screen.width;
    //    float h = theWidget.height / (float)Screen.height;
    //    theCamera.rect = new Rect(l, t, w, h);
    //}


    public void RefreshPano()
    {
        GetBaiduPano();
    }

    bool isActive = false;
    void setActive(bool flag)
    {
        theCamera.enabled = isActive = flag;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F2))
        {
            //setActive(!isActive);
            //if (isActive)
            //{
            //    getPano();
            //}
            //getPano();
        }
    }

    void LateUpdate()
    {
        if (SceneManager.Instance.TheCameraCtrl != null)
        {
            float rotH = SceneManager.Instance.TheCameraCtrl.TheFocusCtrl.RotH + (IsReverse ? 180f : 0f);
            float rotV = VisionRotV;

            theCamera.transform.eulerAngles = new Vector3(rotV, rotH - 90f, 0);
        }
    }

    /// <summary>获取视线倾角(全景图片用)</summary>
    public float VisionRotV
    {
        get
        {
            if (SceneManager.Instance.TheCameraCtrl.CameraState == EnumCameraState.normal)
                return SceneManager.Instance.TheCameraCtrl.TheFocusCtrl.RotV - 50f;
            else if (SceneManager.Instance.TheCameraCtrl.CameraState == EnumCameraState.overshoulder)
                return SceneManager.Instance.TheCameraCtrl.TheFocusCtrl.RotV + 20f;
            else
                return SceneManager.Instance.TheCameraCtrl.TheFocusCtrl.RotV + 20f;
        }
    }

    public bool IsReverse
    {
        get
        {
            return SceneManager.Instance.ConfigJD.ReadBool("街景图像左右翻转");
        }
    }


    void GetBaiduPano()
    {
        // 镜头焦点
        Vector3 focus = SceneManager.Instance.TheCameraCtrl.TheFocusCtrl.FocusPos;
        CityConfig cc = new CityConfig();

        Vector2Double vWorld = new Vector2Double(focus.x, focus.z);

        Vector2Double v2d = cc.PointToCoord(vWorld);

        Debug.Log("相机焦点坐标：" + focus + "--经纬度:" + v2d.ToString());

        string path = "http://api.map.baidu.com/panorama?width=1024&height=512&location=" + v2d.x + "," + v2d.y + "&fov=360&ak=lGAUfSA7xzwVYYo4szKif1Gb";

        HttpService.GetRemoteTexture(path, tex =>
        {
            PanoRenderer.material.mainTexture = tex;
            Vector2 tiling = IsReverse ? new Vector2(-1, 1) : new Vector2(1, 1);
            PanoRenderer.material.SetTextureScale("_MainTex", tiling);
            Debug.Log("获取街景成功:" + tex);
        }, false);
    }
}
